package com.auerdi.ui.detail.sho;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.ui.detail.ADProductDetailFragment;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.GoodLoveList;
import com.example.egghttp.retrofit.beans.goods.Product;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADTwoShowFragMent extends SwipeBackFragment {

    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.recy)
    RecyclerView recy;
    private List<Product> mProLists = new ArrayList<>();

    public static ADTwoShowFragMent newInstance() {

        Bundle args = new Bundle();
        ADTwoShowFragMent fragment = new ADTwoShowFragMent();
        fragment.setArguments(args);
        return fragment;
    }

    Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_two_show, container, false);

        unbinder = ButterKnife.bind(this, view);

        View tpview = attachToSwipeBack(view);


        return tpview;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });


        RetrofitClient.getClient().create(AdService.class).goodsStarLove()
                .subscribeOn(Schedulers.newThread())
                .doOnNext(new Action1<BOb<GoodLoveList>>() {
                    @Override
                    public void call(BOb<GoodLoveList> goodLoveListBOb) {

                        GoodLoveList lists = goodLoveListBOb.getBody();
                        List<Product> ProLists = lists.getProLists();
                        mProLists.clear();
                        mProLists.addAll(ProLists);
                    }
                })
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<GoodLoveList>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(BOb<GoodLoveList> goodLoveListBOb) {

                        recy.getAdapter().notifyDataSetChanged();

                    }
                });
        recy.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recy.setAdapter(new CommonAdapter<Product>(_mActivity, R.layout.item_home_last, mProLists) {
            @Override
            protected void convert(ViewHolder viewHolder, final Product item, int position) {

                ImageView imgView = viewHolder.getView(R.id.img);
                ImageView gPic = viewHolder.getView(R.id.gpic);
                viewHolder.setText(R.id.title, item.getTitle());
                viewHolder.setText(R.id.gname, item.getBnProductGuojiaName());
                viewHolder.setText(R.id.stitle, item.getShareTitle());
                viewHolder.setText(R.id.num, "已团" + item.getPayCount() + "件");


                ImageView tag = viewHolder.getView(R.id.tag);
                viewHolder.setText(R.id.apri, "¥" + item.getSuccessPrice());
                viewHolder.setText(R.id.bpri, "¥" + item.getMarkPrice());
                TextView bb = viewHolder.getView(R.id.bpri);
                bb.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰

                Picasso.get().load(item.getLunboImg()).error(R.mipmap.logo).placeholder(R.mipmap.logo).fit().into(imgView);
                Picasso.get().load(item.getBnProductGuojia()).error(R.mipmap.logo).placeholder(R.mipmap.logo).fit().centerInside().into(gPic);

                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EventBusActivityScope.getDefault((Activity) viewHolder.getConvertView().getContext()).post(ADProductDetailFragment.newInstance(item));

                        L.d("hcia", "item:" + item);

                    }
                });
            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
